package com.groupbuying.orderproducer.service.impl;


import com.groupbuying.orderproducer.constants.OrderUshopEnumConstants;
import com.groupbuying.orderproducer.domain.OrderUshopDO;
import com.groupbuying.orderproducer.service.OrderUshopService;
import com.groupbuying.orderproducer.service.OrderUshopStateService;
import com.groupbuying.orderproducer.utils.apiresult.ApiResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class OrderUshopStateServiceImpl implements OrderUshopStateService {
    @Autowired
    private OrderUshopService orderUshopService;

    @Override
    public ApiResult updateOrderStateByActive(Map<String, Object> mapParams){
        // 解析map参数
        Object tempParam = mapParams.get("orderId");
        String orderId = tempParam == null ? null : tempParam.toString();
        tempParam = mapParams.get("opxRuleId");
        String opxRuleId = tempParam == null ? null : tempParam.toString();
        tempParam = mapParams.get("totalPrice");
        String totalPrice = tempParam == null ? null : tempParam.toString();

        //判断请求参数不为空
        if (StringUtils.isEmpty(orderId) || StringUtils.isEmpty(opxRuleId) || StringUtils.isEmpty(totalPrice)) {
            return ApiResult.paramIsNull();
        }

        OrderUshopDO orderUshopDO = orderUshopService.get(orderId);

        //数据库存在数据且交易状态为进行中
        if (orderUshopDO == null || !OrderUshopEnumConstants.ENUM_DEALSTATE_UNCLOSED.equals(orderUshopDO.getDealState())) {

            return ApiResult.failure();

        }

        orderUshopDO.setGmtModified(new Date());
        orderUshopDO.setOrderState(OrderUshopEnumConstants.ENUM_ORDERSTATE_PAY);
        orderUshopService.update(orderUshopDO);



        return ApiResult.success();
    }

}
